﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Directory.Host;
using Flower.Directory.Host.Default;
using Flower.Services.Data;
using Flower.Testing;
using Flower.Directory.Management;
using NUnit.Framework;

namespace Flower.Directory.Management.Tests
{
    [TestFixture]
    public class FindTests
    {
        private static readonly IScriptEnginePool ScriptEnginePool = new ScriptEnginePool(1);

        [Test]
        public void Find()
        {
            var dir = new MemoryDirectory.Directory(ScriptEnginePool);

            new DirectoryBuilder(dir)
                .Root("/Workflows")
                    .Folder("Folder1").End()
                    .Folder("Folder2").End()
                    .Folder("Folder3").End()
                    .Folder("X")
                        .Folder("AFolder").End()
                        .Folder("BFolder").End()
                        .Folder("CFolder").End()
                        .Folder("X")
                            .Folder("AFolder1").End()
                            .Folder("BFolder2").End()
                            .Folder("CFolder3").End()
                            .Folder("X").End()
                        .End()
                    .End();

            ICollection<DirectoryEntry> entries = dir.Find("/Workflows/*", DirectoryEntryTypes.All);

            Assert.IsTrue(entries.Count == 4);

            var paths = entries.Select(ent => dir.GetPathById(ent.Id)).ToArray();

            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder1"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder2"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder3"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X"));

            entries = dir.Find("/Workflows/Folder*", DirectoryEntryTypes.All);

            Assert.IsTrue(entries.Count == 3);

            paths = entries.Select(ent => dir.GetPathById(ent.Id)).ToArray();

            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder1"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder2"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder3"));

            entries = dir.Find("/Workflows/X/*Folder", DirectoryEntryTypes.All);

            Assert.IsTrue(entries.Count == 3);

            paths = entries.Select(ent => dir.GetPathById(ent.Id)).ToArray();

            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/AFolder"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/BFolder"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/CFolder"));

            entries = dir.Find("/Workflows/**/*Folder*", DirectoryEntryTypes.All);
            paths = entries.Select(ent => dir.GetPathById(ent.Id)).ToArray();

            Assert.IsTrue(entries.Count == 9);
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder1"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder2"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/Folder3"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/AFolder"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/BFolder"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/CFolder"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/X/AFolder1"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/X/BFolder2"));
            Assert.IsTrue(paths.Any(p => p == "/Workflows/X/X/CFolder3"));

            entries = dir.Find("/Workflows/**", DirectoryEntryTypes.All);

            Assert.IsTrue(entries.Count == 12);
        }

        [Test]
        public void FindByType()
        {
            var dir = new MemoryDirectory.Directory(ScriptEnginePool);

            new DirectoryBuilder(dir)
                .Root("/Sets/Shared")
                    .Folder("FindByType")
                        .Set("Set1", "Message", 1).End()
                        .Folder("Folder1")
                            .Set("Set2", "Message", 1).End()
                            .Folder("Folder2")
                                .Set("Set3", "Message", 1)
                                    .Folder("Subset").End()
                                .End()
                            .End()
                        .End()
                    .End();

            ICollection<DirectoryEntry> entries = dir.Find("/Sets/Shared/FindByType/**", DirectoryEntryTypes.Set);

            Assert.AreEqual(3, entries.Count);
            Assert.IsTrue(entries.All(ent => ent is Set));

            entries = dir.Find("/Sets/Shared/FindByType/**", DirectoryEntryTypes.Folder);

            Assert.AreEqual(3, entries.Count);
            Assert.IsTrue(entries.All(ent => ent is Folder));
        }
    }
}
